# @Time : 2024/7/31 17:36
# @Author : Adam
# @Version : v1.0
# @IDE : PyCharm
# @Project : tracer
from django.shortcuts import render, redirect, reverse
from web import models


def setting(request, project_id):
    return render(request, 'setting.html')


def setting_delete(request, project_id):
    """删除项目"""
    if request.method == 'GET':
        return render(request, 'setting_delete.html')

    project_name = request.POST.get('project_name').strip()
    print(project_name)
    print(request.tracer.project.name)

    queryset = models.Project.objects.filter(creator=request.tracer.user, name=project_name)

    if not project_name or not queryset.exists():
        return render(request, 'setting_delete.html', {'error': '项目名错误'})

    # 只有创建者可以删除
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'setting_delete.html', {'error': '只有项目创建者可以删除项目'})
    models.Project.objects.filter(name=project_name).delete()
    return redirect(reverse('project_list'))
